package com.michael.study.component.fragment

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.PagerAdapter

/**
 * Description:
 *
 * Created by liuguoquan on 2020/8/3 23:01.
 */
class AAdapter(fm: FragmentManager, var titles: ArrayList<String>) :
    FragmentPagerAdapter(fm) {

    var fragments = ArrayList<Fragment>()

    override fun getItem(position: Int): Fragment {
        when (titles[position]) {
            "A" -> {
                var f = FragmentA.newInstance()
                fragments.add(f)
                return f
            }
            "B" -> {
                var f = FragmentB.newInstance()
                fragments.add(f)
                return f
            }
            "C" -> {
                var f = FragmentC.newInstance()
                fragments.add(f)
                return f
            }
            "D" -> {
                var f = FragmentD.newInstance()
                fragments.add(f)
                return f
            }
            else -> {
                var f = FragmentA.newInstance()
                fragments.add(f)
                return f
            }
        }
    }

    fun getFragment(index: Int): Fragment {
        return fragments.get(index)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return titles[position]
    }

    override fun getCount(): Int {
        return titles.size
    }
}